package com.anxin.registerManagement.service;

import com.anxin.registerManagement.bean.dto.ReplyDto;
import com.anxin.registerManagement.bean.jo.ReplySearchJo;
import com.anxin.registerManagement.bean.persist.Comment;
import com.anxin.registerManagement.bean.persist.PhotoResource;
import com.anxin.registerManagement.bean.persist.Reply;
import com.anxin.registerManagement.bean.persist.User;
import com.anxin.registerManagement.mapper.CommentMapper;
import com.anxin.registerManagement.mapper.PhotoResourceMapper;
import com.anxin.registerManagement.mapper.ReplyMapper;
import com.anxin.registerManagement.mapper.UserMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.*;

@Service
public class ReplyService {

    private static Logger logger = LoggerFactory.getLogger(ReplyService.class);

    @Autowired
    ReplyMapper replyMapper;

    @Autowired
    PhotoResourceMapper photoResourceMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    CommentMapper commentMapper;

    /**
     * 查询回帖
     */
    public PageInfo<ReplyDto> searchReply(ReplySearchJo replySearchJo) {
        PageHelper.startPage(replySearchJo.getPageNum(), replySearchJo.getPageSize());
        Map<String, Object> searchMap = new HashMap<>();
        searchMap.put("post_bar_id", replySearchJo.getPostBarId());
        List<Reply> replies = replyMapper.selectByMap(searchMap);
        List<ReplyDto> replyDtos = new ArrayList<>();
        for (Reply reply : replies) {
            ReplyDto replyDto = new ReplyDto();
            BeanUtils.copyProperties(reply, replyDto);
            LambdaQueryWrapper<PhotoResource> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(PhotoResource::getId, reply.getReplyUserId());
            PhotoResource photoResource = photoResourceMapper.selectOne(lambdaQueryWrapper);
            User user = userMapper.selectById(reply.getReplyUserId());
            if (!ObjectUtils.isEmpty(photoResource)) {
                replyDto.setPhotoContent(photoResource.getPhotoContent());
            }
            if (!ObjectUtils.isEmpty(user)) {
                replyDto.setNickName(user.getNickName());
            }
            LambdaQueryWrapper<Comment> commentLambdaQueryWrapper = new LambdaQueryWrapper<>();
            commentLambdaQueryWrapper.eq(Comment::getReplyPostBarId,reply.getId());
            List<Comment> comments = commentMapper.selectList(commentLambdaQueryWrapper);
            replyDto.setCommentList(comments);
            replyDtos.add(replyDto);
        }
        return new PageInfo<>(replyDtos);
    }

    /**
     * 添加回帖
     */
    public void insert(Reply reply) {
        replyMapper.insert(reply);
    }

    /**
     * 删除回帖
     */
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Integer> id) {
        // FIXME；这里删除回帖需要删除相应的评论。
        replyMapper.deleteBatchIds(id);
    }
}


